package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.entity.ShoppingCart;
import com.itheima.service.ShoppringCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppringCartService shoppringCartService;

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据:{}", shoppingCart);
        //设置用户id 指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentId);

        if (dishId != null) {
            //添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //查询当前菜品或者套餐是否在购物车中
        ShoppingCart cartServiceOne = shoppringCartService.getOne(queryWrapper);

        if (cartServiceOne != null) {
            //如果已经存在 就在原来的基础上加一
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1);
            shoppringCartService.updateById(cartServiceOne);
        } else {
            //如果不存在 则添加到购物车  数量默认就是一
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppringCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }
        return R.success(cartServiceOne);
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppringCartService.list(queryWrapper);
        return R.success(list);
    }


    /**
     * 购物车删除菜品
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    @Transactional
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据:{}", shoppingCart);
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();

        //dishId 代表删除菜品的数量
        if (dishId != null) {
            //根据id查出购物车中菜品
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
            //判断用户是否操作的是自己的购物车
            queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
            //获取菜品对象
            ShoppingCart cart = shoppringCartService.getOne(queryWrapper);
            //将菜品数量减一
            cart.setNumber(cart.getNumber() - 1);
            //获取菜品的数量 int类型
            Integer cartNumber = cart.getNumber();
            //判断
            if (cartNumber > 0) {
                //大于0  更新购物车菜品数量
                shoppringCartService.updateById(cart);
            }else if (cartNumber == 0){
                //等于0  将菜品从购物车移除
                shoppringCartService.removeById(cart);
            }else if (cartNumber < 0){
                //小于0  说明有异常
                return R.error("操作异常");
            }
            return R.success(cart);
        }

        Long setmealId = shoppingCart.getSetmealId();
        //setmealId 代表删除套餐的数量
        if (setmealId != null) {
            //根据id查出购物车中套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, setmealId);
            //判断用户是否操作的是自己的购物车
            queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
            //获取套餐对象
            ShoppingCart cart2 = shoppringCartService.getOne(queryWrapper);
            //将套餐数量减一
            cart2.setNumber(cart2.getNumber() - 1);
            //获取套餐的数量 int类型
            Integer cartNumber = cart2.getNumber();
            //判断
            if (cartNumber > 0) {
                //大于0  更新购物车套餐数量
                shoppringCartService.updateById(cart2);
            }else if (cartNumber == 0){
                //等于0  将套餐从购物车移除
                shoppringCartService.removeById(cart2);
            }else if (cartNumber < 0){
                //小于0  说明有异常
                return R.error("操作异常");
            }
            return R.success(cart2);
        }

        return R.error("操作异常");
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        shoppringCartService.clean();
        return R.success("购物车已清空");
    }


}
